// redefine jquery
var $J = jQuery.noConflict();

// call dwr service to load the category data
/*------------------------------variable-----------------------*/
var categoryOptionHTML='';
var curPhotos;

/*------------------------------------------------DWR START---------------------------------------------*/
function loadCategory() {
	switchView('list');
	PhotoCategoryService.getAllPhotoCategoryByUserNo(function(categories) {
		var html = getPhotoCategoryHTML(categories);
		$J("#photo_category").html(html);

	});
}
function loadPhoto(code, pageIndex) {
	switchView('list');
	PhotoService.getPhotoByCategoryCode(code, (pageIndex || 0) * 20, 20,
			function(photos) {
				curPhotos=photos;
				var html = getPhotoHTML(photos, code);
				$J('#photo_list').html(html);
			});
	PhotoService.getTotalCountyCategoryCode(code, function(ret) {
		var html=getPagingHTML(code,ret);
		$J('#paging').html(html);
	});
}

function delPhoto(id, code) {
	PhotoService.removePhoto(id, function() {
		loadPhoto(code);
	})
}

function upgradePhoto(id, preId, code) {
	if (preId) {
		PhotoService.upgradePhoto(id, preId, function() {
			loadPhoto(code);
		});
	}
}
function verifyPhoto(id,code) {
	PhotoService.verifyPhoto(id, function() {
		loadPhoto(code);
	})
}

function loadTopPic(userNo){
	PhotoService.getTopPhoto(10000,5,function(ret){
		$J("#topPicList").html(getTopPicHTML(ret));
	})
}

/*------------------------------------------------DWR END---------------------------------------------*/
/*------------------------------------------------HTML START---------------------------------------------*/
function getPhotoCategoryHTML(categories) {
	var html = '';
	for (var i = 0; i < categories.length; i++) {
		
		var category = categories[i];
		var b_begin=i==0?'<b class="b">':'';
		var b_end=i==0?'</b>':'';
		html += '<div class="columnType1ContentLine"><a href="javascript:loadPhoto('
				+ category.code
				+ ')">'+b_begin
				+ category.name
				+ b_end+'</a></div>';
				
				categoryOptionHTML += '<option value=' + category.code + '>' + category.name
				+ '</option>';
		//load the first category photos;
		if(i==0){
			loadPhoto(category.code);
		}
	}
	return html;
}

function getPhotoHTML(photos, code) {
	var html = '';
	for (var i = 0; i < photos.length; i++) {
		var photo = photos[i];
		var preId;
		if (i > 0) {
			preId = photos[i - 1].id
		}

		html += '<div class="picListItem">' + photo.fileName
				+ '<div class="picListItemPicDiv"><a href="javascript:showPhoto('+i+')'
				+ '"><img class="picListItemPic" src="../'
				+ photo.thumbnailUrl + '"/></a></div>'
				+ '<a href="javascript:delPhoto(' + photo.id + ',' + code
				+ ')">删除</a>  <a href="javascript:upgradePhoto(' + photo.id
				+ ',' + preId + ',' + code
				+ ')">升序</a>	<br/>'+(photo.verified?'<a href="javascript:void(0)">已验证照片</a>':'<a href="javascript:verifyPhoto(' + photo.id
				+ ',' + code + ')">设置为验证照片</a>')+'</div>';

	}
	return html;
}

function getTopPicHTML(photos){
	var html = '';
	for (var i = 0; i < photos.length; i++) {
		var photo = photos[i];
		html+='<div class="topPicDiv">'+photo.fileName+'<div class="innerDiv"><a href="">' +
				'<img style="width: 80px;height: 60px;border: 0;" src="../'+photo.thumbnailUrl+'"/></a></div></div>';	
	}
	return html;
}

function getPagingHTML(code,totalCount){
	var html='';
	var pageCount=totalCount/20;
	if(totalCount%20!=0){
		pageCount++;
	}
	for(var i=0;i<pageCount-1;i++){
		html+='<a href="javascript:loadPhoto('+code+','+i+')">'+(i+1)+'</a>&nbsp;&nbsp;&nbsp;';
	}
	return html;
}
/*------------------------------------------------HTML END---------------------------------------------*/
function addCategory(){
		$J.blockUI({
			message : '<div style="valign:middle">'
					+ '相册名称：<input type=text id="categoryName"/><br />'
					+ '是否公开：<input type=checkbox id="isPublic" checked=true/><br />'
					+ '<input type=submit value=确定 onclick="doAddCategory()"><input type=submit value=取消 onclick="$J.unblockUI();"></div>',
			css : {
				border : 'none',
				padding : '15px',
				'-webkit-border-radius' : '10px',
				'-moz-border-radius' : '10px',
				opacity : '.5',
				cursor : 'pointer'
			}
		})
}

function delCategory(){
		$J.blockUI({
			message : '<iframe src=photo.jsp height=200px width=100% frameborder=0/>',
			css:{
			 cursor:'pointer',
			 '-webkit-border-radius' : '10px',
			'-moz-border-radius' : '10px',
			opacity : '.5'
			}

		})
	}
function uploadPhoto() {
		$J("#categorySelect").html(categoryOptionHTML);
		$J.blockUI({
			message : $J("#uploadFormDiv"),
			css:{
			 cursor:'pointer',
			 '-webkit-border-radius' : '10px',
			'-moz-border-radius' : '10px',
			opacity : '.5'
			}

		})
	}
	
function doAddCategory() {
	var category = {};
	category.name = $J("#categoryName").val();
	category.isPublic = $J("#isPublic").attr("checked");
	PhotoCategoryService.saveOrUpdatePhotoCategory(category, function() {
		loadCategory();
		$J.unblockUI();
	})
}

function doUploadPhoto(event) {
	if(validate()){
	$J.unblockUI();

	$J.blockUI({
		message : '文件上传中，请稍候...',
		css : {
			border : 'none',
			padding : '15px',
			backgroundColor : '#000',
			'-webkit-border-radius' : '10px',
			'-moz-border-radius' : '10px',
			opacity : '.5',
			color : '#fff'
		}
	});
	}
	else{
		event.preventDefault();
	}
	
}
function uploadCallback(msg) {
	loadCategory();
	 $J.unblockUI();
	 alert(msg);
}

function showPhoto(idx){
	switchView('share');
	var html;
	var photo;
	//add prePhotoHTML
	if(idx>0){
		photo=curPhotos[idx-1];
			 html= '<div class="picListItem">' + photo.fileName
				+ '<div class="picListItemPicDiv"><a href="javascript:showPhoto('+(idx-1)+')'
				+ '"><img class="picListItemPic" src="../'
				+ photo.thumbnailUrl + '"/></a></div>'+'</div>';
			$J("#prePic").html(html);
		
	}
	else{
		$J("#prePic").html('');
	}
	//add nextPhotoHTML
	if(idx<curPhotos.length-1){
			photo=curPhotos[idx+1];
			html = '<div class="picListItem">' + photo.fileName
				+ '<div class="picListItemPicDiv"><a href="javascript:showPhoto('+(idx+1)+')'
				+ '"><img class="picListItemPic" src="../'
				+ photo.thumbnailUrl + '"/></a></div>'+'</div>';
			$J("#nextPic").html(html);
	}
	else{
		$J("#nextPic").html('');
	}
	//add photo HTML
	photo=curPhotos[idx];
	//increase the photo's view count
	PhotoService.increaseViewCount(photo.id);
	html='<img src="../'+photo.url+'" border="0"/>';
	$J("#curPic").html(html);
	new Rating("rating",1,1,'../');
	
}
/*-------------------------------------------------------------------------------------------------------*/
function validate() {
	var allowedTypes = ['jpg', 'gif', 'png'];
	var flag = true;
	$J(":file").each(function() {
		var fileName = this.value;
		if (fileName) {
			var idx = fileName.lastIndexOf('.');
			var sufix = fileName.substring(idx + 1);
			for (var i = 0; i < allowedTypes.length; i++) {
				if (sufix == allowedTypes[i]) {
					return ;
				}
			}
			alert(fileName + " 格式不对.\n只支持jpg,gif,png.");
			flag = false;
		}
	});
	return flag;
}

function switchView(type){
	if(type=='list'){
		$J("#picShare").hide();
		$J("#picViewer").hide();
		
		$J("#picList").show();
		$J("#topPic").show();
		
	}
	else{
		$J("#picShare").show();
		$J("#picViewer").show();
		$J("#picList").hide();
		$J("#topPic").hide();
	}
}
function bindEvent(){
	$J("#uploadForm").bind('submit', doUploadPhoto);
}
/*-------------------------------------------------------------------------------------------------------*/

$J(loadCategory);
$J(loadTopPic);
$J(bindEvent);


